using System.ComponentModel;
using System.Runtime.CompilerServices;
using SageKing.Designer.Core;
using SageKing.Designer.Core.Activities.Flowchart.Contracts;
using SageKing.Designer.Core.Activities.Flowchart.Extensions;
using SageKing.Designer.Core.Activities.Flowchart.Models;
using SageKing.Designer.Core.Attributes;
using SageKing.Designer.Core.Contracts; 
using Microsoft.Extensions.Logging;
using Connection = SageKing.Designer.Core.Activities.Flowchart.Models.Connection;

namespace SageKing.Designer.Core.Activities.Flowchart.Activities;

/// <summary>
/// A flowchart consists of a collection of activities and connections between them.
/// </summary>
[Activity("SageKing", "Flow", "A flowchart is a collection of activities and connections between them.")]
[Browsable(false)]
public class Flowchart : Container
{
    public const string ScopeProperty = "Scope";

    /// <inheritdoc />
    public Flowchart([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
    { 
    }

    /// <summary>
    /// The activity to execute when the flowchart starts.
    /// </summary>
    [Port]
    [Browsable(false)]
    public IActivity? Start { get; set; }

    /// <summary>
    /// A list of connections between activities.
    /// </summary>
    public ICollection<Connection> Connections { get; set; } = new List<Connection>();

     

    private IActivity? GetRootActivity()
    {
        // Get the first activity that has no inbound connections.
        var query =
            from activity in Activities
            let inboundConnections = Connections.Any(x => x.Target.Activity == activity)
            where !inboundConnections
            select activity;

        var rootActivity = query.FirstOrDefault();
        return rootActivity;
    } 
     
}